package widgets;
/**
 *  JPanel containing the dataset produced by each run
 *
 *  Copyright (c) 2009 Daniel Kornhauser.  All rights reserved.
 *  @author Daniel Kornhauser
 */

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;


public class DataSetPanel extends JPanel implements TableModelListener, ActionListener{
	private static final long serialVersionUID = 1L;
	private final int TOTAL_COLUMNS = 3;
	private JTable table;
	private DataSetTableModel model;
	private List <Object> data;
	public int datasetNumber = 1;
	
	public DataSetPanel() {
		initialize();
	}

	private void initialize() {
		data = new ArrayList();
//	//Leaving the code below in case I evet want to populate the datasets
//		for (int i = 0; i < 10; i++) {
//			Object record[] = new Object[COLUMN_COUNT];
//			record[0] = Boolean.FALSE;
//			for (int j = 1; j < COLUMN_COUNT; j++) {
//				record[j] = new Integer(j);
//			}
//			data.add(record);
//		}
		model = new DataSetTableModel(data);
		table = new JTable(model);
		table.getSelectionModel().setSelectionMode(
				ListSelectionModel.SINGLE_SELECTION);
		table.getModel().addTableModelListener(this);
		table.removeColumn(table.getColumnModel().getColumn(2));
		
		Object record[] = new Object[TOTAL_COLUMNS];
		record[0] = Boolean.FALSE;
		record[1] = new String("Test");
		record[2] = new Integer(datasetNumber);
		data.add(record);
		model.fireTableDataChanged();
		
		JScrollPane scroll = new JScrollPane(table);
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		panel.add(scroll);
		JButton addDataSetButton = new JButton("Add Dataset");
		addDataSetButton.setActionCommand("Add Dataset");
		addDataSetButton.addActionListener(this);
		panel.add(addDataSetButton);
		JButton removeDataSetButton = new JButton("Remove Dataset");
		removeDataSetButton.setActionCommand("Remove Dataset");
		removeDataSetButton.addActionListener(this);
		panel.add(removeDataSetButton);
		add(panel);
	}
	
	public void actionPerformed(ActionEvent e) {
		System.out.println(e.getSource());
		if (e.getActionCommand().equals("Add Dataset"))
		{
			Object record[] = new Object[TOTAL_COLUMNS];
			record[0] = Boolean.FALSE;
			record[1] = new String("Test");
			record[2] = new Integer(datasetNumber);
			data.add(record);
			model.fireTableDataChanged();
		}
		else if (e.getActionCommand().equals("Remove Dataset"))
		{
			int selectedRow = table.getSelectedRow();
			data.remove(selectedRow);
			model.fireTableRowsDeleted(selectedRow, selectedRow);
		}
	}

	public static void main(String[] args) {
	     JFrame frame = new JFrame();
	     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		DataSetPanel dataSetPanel = new DataSetPanel();
		frame.getContentPane().add(dataSetPanel);
		frame.pack();
		frame.setVisible(true);
	}

	class DataSetTableModel extends AbstractTableModel {
		private static final long serialVersionUID = 1L;
		private List <Object> data;

		public DataSetTableModel(List <Object> data) {
			this.data = data;
		}

		public int getColumnCount() {
			return TOTAL_COLUMNS;
		}

		public int getRowCount() {
			return data == null ? 0 : data.size();
		}

		public void setValueAt(Object value, int rowIndex, int columnIndex) {
			getRecord(rowIndex)[columnIndex] = value;
			super.fireTableCellUpdated(rowIndex, columnIndex);
		}

		public Object getValueAt(int rowIndex, int columnIndex) {
			return getRecord(rowIndex)[columnIndex];
		}

		public boolean isCellEditable(int rowIndex, int columnIndex) {
			return true;
		}

		public Class getColumnClass(int columnIndex) {
			if (data == null || data.size() == 0) {
				return Object.class;
			}
			Object o = getValueAt(0, columnIndex);
			return o == null ? Object.class : o.getClass();
		}

		private Object[] getRecord(int rowIndex) {
			return (Object[]) data.get(rowIndex);
		}
	}

	public void tableChanged(TableModelEvent e) {
        System.out.println(Arrays.deepToString(data.toArray()));
 //   // Leaving example code in case I need it later ...       
//		if (e.getType() == TableModelEvent.UPDATE) {
//            int column = e.getColumn();
//            int row = e.getFirstRow();
//            System.out.println("row: " + row + " column: " + column);
//            if (column >= 0)
//            {
//            	System.out.println(table.getValueAt(row, column));
//            }
//		}
		
	}
}